﻿using System;
using Csla.Xaml;
using Accounting.Library;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;



namespace csla4Skeleton.UI.SL.NghiepVu.Kho.K_PhieuNhapKho
{
    public class K_PhieuNhapKho_ViewModel : ViewModel<Accounting.Library.K_PhieuNhapKho>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
        public delegate void TKNoIdStartWithSix_EventHandler(object sender, TKNoIdStartWithSixEventArgs e);
        public delegate void DoiTuongTHCPIDChanged_EventHandler(object sender, DoiTuongTHCPIDChangedEventArgs e);

        public delegate void HangHoaIdChanged_EventHandler(object sender, HangHoaIdChangedEventArgs e);
       
        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;

        public event TKNoIdStartWithSix_EventHandler TKNoIdStartWithSix;
        public event EventHandler LoadCompleted;
        public event EventHandler Saved;
        public event EventHandler Canceled;
        public event EventHandler TCHangHoaChanged;
        public event EventHandler LoaiTienTeChanged;
        public event HangHoaIdChanged_EventHandler hangHoaIdChanged;
        public event DoiTuongTHCPIDChanged_EventHandler DoiTuongTHCPIDChanged;
        

        #endregion

        #region Refresh Model

        public K_PhieuNhapKho_ViewModel(string mode)
        {
            if (mode.Equals(BusinessConfiguration.Configuration.ChildWindow))
            {
                this.BeginRefresh("NewK_PhieuNhapKho");
            }
           
        }
        public K_PhieuNhapKho_ViewModel(int ChungTuThayTheId, int? LoaiChungTuThayTheId)
        {

            this.BeginRefresh("GetK_PhieuNhapKho", ChungTuThayTheId.ToString() + ";" + LoaiChungTuThayTheId.ToString());

        }
        public K_PhieuNhapKho_ViewModel()
        {
            BeginRefresh("NewK_PhieuNhapKho");
            
        }

        public K_PhieuNhapKho_ViewModel(int Khoa)
        {
            BeginRefresh("GetK_PhieuNhapKho", Khoa);
           
        }

        public void NewK_PhieuNhapKhoViewModel()
        {
            BeginRefresh("NewK_PhieuNhapKho");
            
        }

        public void GetK_PhieuNhapKhoViewModel(int Khoa)
        {
            BeginRefresh("GetK_PhieuNhapKho", Khoa);
        }

       
        public void SaveAndNew()
        {
            BeginSave();
            NewK_PhieuNhapKhoViewModel();
        }
        public void SavePhieuNhapKho()
        {
            BeginSave();
           
        }
        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.K_PhieuNhapKho_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<K_PhieuNhapKho_ChiTietChild>>(K_PhieuNhapKho_ChiTiets_AddedNew);
               
                Model.TinhChatHangHoaChanged += new EventHandler(OnTinhChatHangHoaChanged);
                Model.HangHoaIdChanged += new EventHandler<HangHoaIdChangedEventArgs>(OnHangHoaIdChanged);
                Model.TKNoIdStartWithSix += new EventHandler<TKNoIdStartWithSixEventArgs>(OnTKNoIdStartWithSix);
                Model.LoaiTienTeChanged+= new EventHandler(OnLoaiTienTeChanged);
                Model.DoiTuongTHCPIDChanged += new EventHandler<DoiTuongTHCPIDChangedEventArgs>(OnDoiTuongTHCPIDChanged);
                if (LoadCompleted != null)
                {
                    LoadCompleted(this, new EventArgs());
                }
            }
        }
        #endregion
        
        #region Add K_PhieuNhapKho Details Item
        public void AddK_PhieuNhapKho_ChiTietItem()
        {
            Model.K_PhieuNhapKho_ChiTiets.AddNewItem(Model.Khoa);
        }

       

        void K_PhieuNhapKho_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<K_PhieuNhapKho_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("K_PhieuNhapKho_ChiTiets"));
        }      

       

        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }

      
        public void OnHangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            if (hangHoaIdChanged != null)
            {
                hangHoaIdChanged(sender, e);
            }

        }
        public void OnTinhChatHangHoaChanged(object sender, EventArgs e)
        {
            if (TCHangHoaChanged != null)
            {
                TCHangHoaChanged(sender, e);
            }

        }

        public void OnDoiTuongTHCPIDChanged(object sender, DoiTuongTHCPIDChangedEventArgs e)
        {
            if (DoiTuongTHCPIDChanged != null)
            {
                DoiTuongTHCPIDChanged(sender, e);
            }

        }
        public void OnTKNoIdStartWithSix(object sender, TKNoIdStartWithSixEventArgs e)
        {
            if (TKNoIdStartWithSix != null)
            {
                TKNoIdStartWithSix(sender, e);
            }

        }
        public void OnLoaiTienTeChanged(object sender, EventArgs e)
        {
            if (LoaiTienTeChanged != null)
            {
                LoaiTienTeChanged(sender, e);
            }
        }

        public void DoChangingBusy(bool busy)
        {
            IsBusy = busy;
        }

        protected override void OnSaved()
        {
            base.OnSaved();
            if (Saved != null)
            {
                Saved(this, EventArgs.Empty);
            }
        }

        protected override void DoCancel()
        {
            base.DoCancel();
            if (Canceled != null)
            {
                Canceled(this, EventArgs.Empty);
            }
        }


        #endregion

        #region Delete K_PhieuNhapKho Details Item

        public void DeleteK_PhieuNhapKho_ChiTietItem(int Index)
        {
            Model.K_PhieuNhapKho_ChiTiets.DeleteItem(Index);
        }

        #endregion

        #region Nghiep Vu So
        internal void GhiSo()
        {
            this.Model.SaveMode = BusinessConfiguration.Configuration.GhiSoSaveMode;
            BeginSave();
        }

        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_K_PhieuNhapKho;
            int chungTuGocId = Model.Khoa;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;
                BeginRefresh("GetK_PhieuNhapKho", Model.Khoa);
            });
            this.IsBusy = false;
        }
#endregion
    }
}
